package com.hmyd.hmyd_android.view;

import com.hmyd.hmyd_android.utils.Utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 可倾斜的textview，为了兼容低版本，所以额外写一个控件
 * @author wangk
 *
 */
public class InclineTextView extends View {
	
	private TextPaint textPaint;
	private Paint bgPaint;
	private Paint windowsTransPaint;
	
	private int textColor;
	private int bgColor;
	private int windowsTransBgColor;
	
	private float textSize;
	
	private Rect bgRect;
	
	private int textWidth;
	private int textHeight;
	
	private Point textPoint;
	
	// wheather is bold
	private boolean isBold;
	
	// text
	private CharSequence text;
	
	/**
	 * 倾斜度数，默认-45度
	 */
	private float inclineDegree = -45f;

	public InclineTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initTools();
	}

	public InclineTextView(Context context) {
		super(context);
		initTools();
	}

	public InclineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initTools();
	}
	

	private void initTools() {
		textColor = Color.WHITE;
		bgColor = Color.RED;
		windowsTransBgColor = Color.WHITE;
		text = " ";
		isBold = true;
		
		textSize = Utils.getRawSize(getContext(), TypedValue.COMPLEX_UNIT_SP, 14);
		
		textPaint = new TextPaint();
		bgPaint = new Paint();
		windowsTransPaint = new Paint();
		bgRect = new Rect();
		textPoint = new Point();
		
		// 抗锯齿
		textPaint.setAntiAlias(true);
		bgPaint.setAntiAlias(true);
		windowsTransPaint.setAntiAlias(true);
		
		initToolsParams();
	}
	
	private void initToolsParams() {
		// color
		textPaint.setColor(getTextColor());
		bgPaint.setColor(getBgColor());
		windowsTransPaint.setColor(windowsTransBgColor);
		setBackgroundColor(Color.TRANSPARENT);
		
		// bold
		textPaint.setFakeBoldText(isBold);
		
		// size
		textPaint.setTextSize(textSize);
		
		textPaint.setTextAlign(Align.CENTER);
		
		FontMetrics fm = textPaint.getFontMetrics();
		
		textHeight = (int) (Math.ceil(fm.bottom-fm.top)+2);
		textWidth = (int) textPaint.measureText((String) text);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.saveLayer(0, 0, getMeasuredWidth(), getMeasuredHeight(), windowsTransPaint, Canvas.ALL_SAVE_FLAG);
		canvas.rotate(inclineDegree, getMeasuredWidth()/2,getMeasuredHeight()/2);
		canvas.drawRect(bgRect, bgPaint);
		canvas.drawText((String) text, textPoint.x, textPoint.y, textPaint);
		canvas.restore();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		
		// set center vertical
		bgRect.left = getPaddingLeft();
		bgRect.top = getMeasuredHeight()/2-textHeight/2;
		bgRect.right = widthSize-getPaddingRight();
		bgRect.bottom = getMeasuredHeight()/2+textHeight/2;
		
		textPoint.x = getMeasuredWidth()/2;
		textPoint.y = getMeasuredHeight()/2+textHeight/4;
	}

	public float getInclineDegree() {
		return inclineDegree;
	}

	public void setInclineDegree(float inclineDegree) {
		this.inclineDegree = inclineDegree;
		notifyDataChanged();
	}
	
	/**
	 * set text
	 * @param charsequence
	 */
	public void setText(CharSequence charsequence) {
		this.text = charsequence;
		notifyDataChanged();
	}
	
	public CharSequence getText() {
		return text;
	}
	
	public void setTextColor(int color) {
		this.textColor = color;
		notifyDataChanged();
	}
	
	public int getTextColor() {
		return textColor;
	}
	
	public int getBgColor() {
		return bgColor;
	}
	
	public void setBgColor(int color) {
		this.bgColor = color;
		notifyDataChanged();
	}
	
	public void setBold(boolean bold) {
		this.isBold = bold;
		notifyDataChanged();
	}
	
	public void setTextSize(float size) {
		this.textSize = size;
		notifyDataChanged();
	}
	
	public void notifyDataChanged() {
		initToolsParams();
		invalidate();
	}

}
